package com.inepex.inechart.client.displayengine;

import java.util.ArrayList;
import java.util.TreeMap;

import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Widget;
import com.inepex.inegraphics.impl.client.DrawingAreaGWT;

/**
 * {@link RectangleTile} implementation to be used in {@link DisplayEngine} 
 * 
 * @author Miklos Suveges
 *
 */
public class CartesianRectangleTile extends RectangleTile {

	private final AbsolutePanel panel;
	protected ArrayList<DrawingAreaGWT> drawingAreaGWTs;
	protected TreeMap<String, DrawingAreaGWT> canvasPerName;
	protected double xFrom, xTo, yFrom, yTo;
	protected boolean firstDisplay;
	
	public CartesianRectangleTile(){
		panel = new AbsolutePanel();
		drawingAreaGWTs = new ArrayList<DrawingAreaGWT>();
	}
	
	public CartesianRectangleTile(double xFrom, double xTo, double yFrom,
			double yTo) {
		super();
		panel = new AbsolutePanel();
		drawingAreaGWTs = new ArrayList<DrawingAreaGWT>();
		canvasPerName = new TreeMap<String, DrawingAreaGWT>();
		this.xFrom = xFrom;
		this.xTo = xTo;
		this.yFrom = yFrom;
		this.yTo = yTo;
		firstDisplay = false;
	}

	public CartesianRectangleTile(int row, int column, int width, int height) {
		super(row, column, width, height);
		panel = new AbsolutePanel();
		canvasPerName = new TreeMap<String, DrawingAreaGWT>();
		drawingAreaGWTs = new ArrayList<DrawingAreaGWT>();
		setSize(width, height);
		firstDisplay = false;
	}

	@Override
	public Widget asWidget() {
		return panel;
	}
	
	public DrawingAreaGWT createDrawingAreaGWT(){
		DrawingAreaGWT dag = new DrawingAreaGWT(width, height);
		panel.add(dag.getWidget(), 0, 0);
		return dag;
	}
	
	public DrawingAreaGWT createDrawingAreaGWT(String name){
		DrawingAreaGWT dag = createDrawingAreaGWT();
		canvasPerName.put(name, dag);
		return dag;
	}
	
	public DrawingAreaGWT getDrawingAreaGWTByName(String name){
		return canvasPerName.get(name);
	}

	@Override
	public void setSize(int width, int height){
		this.height = height;
		this.width = width;
		panel.setPixelSize(width, height);
		for (DrawingAreaGWT dag : drawingAreaGWTs) {
			dag.setSize(width, height);
		}
	}
	
	public AbsolutePanel getPanel() {
		return panel;
	}

	public double getxFrom() {
		return xFrom;
	}

	public void setxFrom(double xFrom) {
		this.xFrom = xFrom;
	}

	public double getxTo() {
		return xTo;
	}

	public void setxTo(double xTo) {
		this.xTo = xTo;
	}

	public double getyFrom() {
		return yFrom;
	}

	public void setyFrom(double yFrom) {
		this.yFrom = yFrom;
	}

	public double getyTo() {
		return yTo;
	}

	public void setyTo(double yTo) {
		this.yTo = yTo;
	}

	public boolean isFirstDisplay() {
		return firstDisplay;
	}

	public void setFirstDisplay(boolean firstDisplay) {
		this.firstDisplay = firstDisplay;
	}	
	
	public void reset(){
		firstDisplay = true;
		panel.clear();
	}	
	
}
